package com.financing.wap.web.controller.api;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.financing.mail.dto.MailContentDTO;
import com.financing.mail.service.MailSenderService;
import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.common.ResultMap.STATUS;
import com.financing.wap.common.Session;
import com.financing.wap.common.SessionUtil;
import com.financing.wap.dto.ForegroundProductDTO;
import com.financing.wap.dto.MessageMailDTO;
import com.financing.wap.dto.OutProductDTO;
import com.financing.wap.dto.ProductSupplierDTO;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.service.IForegroundProductService;
import com.financing.wap.service.IMessageService;
import com.financing.wap.service.IProductSupplierService;
import com.financing.wap.service.ISmsNewService;
import com.financing.wap.util.Constants;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.util.FileGzipUtil;
import com.financing.wap.util.MessageUtil;
import com.financing.wap.util.PropertiesUtils;
import com.financing.wap.web.controller.BaseController;

@Controller
@RequestMapping("/api/uploadProduct")
public class UploadProductController extends BaseController {
	Logger logger = Logger.getLogger(getClass());

	@Autowired
	private IProductSupplierService productSupplierService;
	
	@Autowired
	private IForegroundProductService foregroundProductService;
	
	@Autowired
	private IMessageService messageService;
	
	@Autowired
	public ISmsNewService smsNewService;
	
	@Resource(name="EmailSendServiceImp")
	private MailSenderService mailSenderService;
	
	String uploadProductPhoneTo=PropertiesUtils.getValue("uploadProduct.phone.to");

	String uploadProductEmailTo=PropertiesUtils.getValue("uploadProduct.email.send.to");
	String uploadProductEmailCopyTo=PropertiesUtils.getValue("uploadProduct.email.send.copy.to");
	
	
	@RequestMapping("addProduct")
	@ResponseBody
	public ResultMap addProduct(HttpServletRequest request, HttpServletResponse response,ProductSupplierDTO productSupplierParam,ForegroundProductDTO foregroundProductDto){
		Session session = SessionUtil.getSession();
		UserDTO loginUser = session.getUser();
		//当前登录人ID
		String faId = loginUser.getId();
		
		ResultMap result = new ResultMap();
		
		String platformtype = request.getParameter(Constants._PT);
		if(StringUtils.isBlank(platformtype)){
			platformtype="pc";//默认认为pc
		}
		foregroundProductDto.setProductSource(platformtype);
		
		String rebateRate = foregroundProductDto.getRebateRate();
		String rebateType = foregroundProductDto.getRebateType();
		String flagAnonymous=foregroundProductDto.getFlagAnonymous();
		String productName = foregroundProductDto.getProductName();
		
		//默认不匿名
		if(StringUtils.isBlank(flagAnonymous)){
			foregroundProductDto.setFlagAnonymous("2");
		}
		
		try {
			if (StringUtils.isBlank(productName)){
				// 参数缺失
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				return result;
			}
			
			boolean flagPC = platformtype.equalsIgnoreCase("pc");
			if(!flagPC){
				//返佣类型和 返佣
				boolean b = StringUtils.isBlank(rebateType) || StringUtils.isBlank(rebateRate);
				if(b){
					// 参数缺失
					result.setStatus(STATUS.ERROR);
					result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
					return result;
				}else if(rebateRate.contains("%")){
					// 参数缺失
					result.setStatus(STATUS.ERROR);
					result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_TYPE_ERROR);
					return result;
				}
				
				//返佣类型 为固定返佣
				if("1".equals(rebateType)){
					try {
						if(!rebateRate.contains("[")){
							foregroundProductDto.setRebateRate("["+rebateRate+"]");
							rebateRate="["+rebateRate+"]";
						}
						JSONArray json=JSONArray.parseArray(rebateRate);
						foregroundProductDto.setRebateRate(json.toJSONString());
					} catch (Exception e) {
						result.setStatus(STATUS.ERROR);
						result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
						return result;
					}
				}
				
				//初始化名字和手机号
				String mobile = loginUser.getLoginName();
				String name=loginUser.getUserName();
				String userName=StringUtils.isBlank(name)?mobile:name;
				productSupplierParam.setName(userName);
				productSupplierParam.setMobile(mobile);
			}
			
			/*非正常用户*/
			String faStatus = loginUser.getFaStatus();
			
			if("02".equals(faStatus)||"03".equals(faStatus)){//非正常用户
				// 参数缺失
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_ACCOUNT_DISABLE);
				return result;
			}else if("04".equals(faStatus)){
				// 参数缺失
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_ACCOUNT_BLACK);
				return result;
			}
			
			/*查询该用户是否审核通过*/
			String role = loginUser.getRole();
			if("98".equals(role)||"99".equals(role)||"1".equals(role)){
				// 参数缺失
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_USER_CHECK_NOT_PASS);
				return result;
			}
			
			ProductSupplierDTO productSupplierDto=new ProductSupplierDTO();
			productSupplierDto.setFaId(String.valueOf(faId));
			List<ProductSupplierDTO> queryProductSupplier = productSupplierService.queryProductSupplier(productSupplierDto);
			
			String name = productSupplierParam.getName();
			//已存在供应方信息
			if(queryProductSupplier.size()>0){
				logger.info("非第一次上传....");
				ProductSupplierDTO productSupplier = queryProductSupplier.get(0);
				//黑名单
				if("1".equals(productSupplier.getBlackList())){
					logger.error("==>warn getBlackList is 1 ");
					result.setStatus(STATUS.ERROR);
					result.setStatusCode(ResultMap.STATUS_CODE_ACCOUNT_BLACK);
					return result;
				}
				productSupplierParam.setId(productSupplier.getId());
			}else{
				//个人供应方信息
				String mobile = productSupplierParam.getMobile();
				if(StringUtils.isBlank(name)||StringUtils.isBlank(mobile)){
					result.setStatus(STATUS.ERROR);
					result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
					return result;
				}
			}
			
			productSupplierParam.setFaId(productSupplierDto.getFaId());
			DTOdateSetUtil.setUpdateInfo(productSupplierParam);
			DTOdateSetUtil.setUpdateInfo(foregroundProductDto);
			DTOdateSetUtil.setCreateInfo(productSupplierParam);
			DTOdateSetUtil.setCreateInfo(foregroundProductDto);
			//没有产品附件 
			String productAttr = foregroundProductDto.getProductAttr();
			if(flagPC&&StringUtils.isBlank(productAttr)){
				logger.error("==>warn getProductAttr is null ");
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_VALIDATE_ERROR);
				return result;
			}
			//有产品附件 就压缩
			if(!StringUtils.isBlank(productAttr)){
				//新增disk绝对目录
				String resultAttr=FileGzipUtil.fileGzip(productAttr.split(","));
				foregroundProductDto.setProductAttr(resultAttr.replace("/disk", ""));
			}
			//新增产品个人供应方和产品
			productSupplierService.addProductSupplierAndProduct(productSupplierParam, foregroundProductDto);
			
			if(platformtype.equalsIgnoreCase("wap")){
				try {
					List<String> mobileList=new ArrayList<String>();
					for (String phone : uploadProductPhoneTo.split(";")) {
						if(!StringUtils.isBlank(phone.trim()))
							mobileList.add(phone);
					}
					//发送短信和邮件
					String content="%s用户上传【%s】产品，该用户手机号码%s，请及时审核";
					content=String.format(content, name,productName,loginUser.getLoginName());
					smsNewService.sendSms(mobileList,content,"smsSendSwitch");
					
					//发送邮件
					MailContentDTO mail = new MailContentDTO();
					mail.setRecAddress(uploadProductEmailTo);
					mail.setCc(uploadProductEmailCopyTo);
					mail.setSubject("用户上传产品");
					mail.setContext(content);
					mailSenderService.send(mail);
				} catch (Exception e) {
					log.error("上传产品发送短信,邮件异常",e);
				}

				
			}
			
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		}catch(Exception e) {
			logger.warn("==>warn messang:",e);
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return result;
	} 
	
	/**
	 * 修改前台产品的信息
	 * @param request
	 * @param response
	 * @param productSupplierParam
	 * @param foregroundProductDto
	 * @return
	 */
	@RequestMapping("editProduct")
	@ResponseBody
	public ResultMap editProduct(HttpServletRequest request, HttpServletResponse response,
			ProductSupplierDTO productSupplierParam,ForegroundProductDTO foregroundProductDto){
		Session session = SessionUtil.getSession();
		UserDTO loginUser = session.getUser();
		//当前登录人ID
		String faId = loginUser.getId();
		ResultMap result = new ResultMap();
		String platformtype = request.getParameter(Constants._PT);
		if(StringUtils.isBlank(platformtype)){
			platformtype="pc";//默认认为pc
		}
		foregroundProductDto.setProductSource(platformtype);
		String productSource = foregroundProductDto.getProductSource();
		String productName = foregroundProductDto.getProductName();
		Integer id = foregroundProductDto.getId();
		boolean flagPC = platformtype.equalsIgnoreCase("pc");
		String flagAnonymous = foregroundProductDto.getFlagAnonymous();
		String rebateType = foregroundProductDto.getRebateType();
		String rebateRate = foregroundProductDto.getRebateRate();
		try {
			if(StringUtils.isBlank(productSource)||!(id > 0 && StringUtils.isNotBlank(productName))){
				// 参数缺失
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				return result;
			}
			
			boolean b = StringUtils.isBlank(flagAnonymous)|| StringUtils.isBlank(rebateType)|| StringUtils.isBlank(rebateRate);
			if(!flagPC){
				if(b){
					// 参数缺失
					result.setStatus(STATUS.ERROR);
					result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
					return result;
				}
			}
			
			// 根据id查询产品，判断其状态是否为审核未通过。
			List<ForegroundProductDTO> fp = foregroundProductService.queryForegroundProduct(foregroundProductDto);
			
			if(fp != null && fp.size() > 0){
				ForegroundProductDTO p = fp.get(0);
				String checkStatus = p.getCheckStatus();
				if(!checkStatus.equals("3")){
					// 违法操作，因为此种情况只能在非正常流程中出现，所以就让页面跳转到/user/center
					result.setStatus(STATUS.ERROR);
					result.setStatusCode(ResultMap.STATUS_CODE_OPERATE_ERROE);
					return result;
				}
			}else if(fp == null || fp.size() <= 0){
				// 违法操作，因为此种情况只能在非正常流程中出现，所以就让页面跳转到/user/center
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_OPERATE_ERROE);
				return result;
			}
			
			ProductSupplierDTO productSupplierDto=new ProductSupplierDTO();
			productSupplierDto.setFaId(faId==null?"":String.valueOf(faId));
			List<ProductSupplierDTO> queryProductSupplier = productSupplierService.queryProductSupplier(productSupplierDto);
			//已存在供应方信息
			if(queryProductSupplier.size()>0){
				logger.info("非第一次上传....");
				ProductSupplierDTO productSupplier = queryProductSupplier.get(0);
				//黑名单
				if("1".equals(productSupplier.getBlackList())){
					logger.error("==>warn getBlackList is 1 ");
					result.setStatus(STATUS.ERROR);
					result.setStatusCode(ResultMap.STATUS_CODE_ACCOUNT_BLACK);
					return result;
				}
				productSupplierParam.setId(productSupplier.getId());
			}
			productSupplierParam.setFaId(productSupplierDto.getFaId());
			DTOdateSetUtil.setUpdateInfo(productSupplierParam);
			DTOdateSetUtil.setUpdateInfo(foregroundProductDto);
			DTOdateSetUtil.setCreateInfo(productSupplierParam);
			DTOdateSetUtil.setCreateInfo(foregroundProductDto);
			
			//没有产品附件    app 不检查
			String productAttr = foregroundProductDto.getProductAttr();
			if(flagPC&&StringUtils.isBlank(productAttr)){
				logger.error("==>warn getProductAttr is null ");
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_VALIDATE_ERROR);
				return result;
			}
			
			//有产品附件 就压缩
			if(!StringUtils.isBlank(productAttr)){
				//新增disk绝对目录
				String resultAttr=FileGzipUtil.fileGzip(productAttr.split(","));
				foregroundProductDto.setProductAttr(resultAttr.replace("/disk", ""));
			}
			
			// 修改后产品状态变成待审核
			foregroundProductDto.setCheckStatus("0");
			
			// 修改产品
			productSupplierService.updateForegroundProduct(foregroundProductDto);
			
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		}catch(Exception e) {
			logger.warn("==>warn messang:",e);
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return result;
	} 
	
	
	/**
	 * 
	* @Title: queryProductSupplierInfo
	
	* @Description:查询产品供应方信息
	
	* @param @param request
	* @param @param response
	* @param @return    设定文件
	
	* @return ResultMap    返回类型
	
	* @throws
	 */
	@RequestMapping("queryProductSupplierInfo")
	@ResponseBody
	public ResultMap queryProductSupplierInfo(HttpServletRequest request, HttpServletResponse response){
		Session session = SessionUtil.getSession();
		UserDTO loginUser = session.getUser();
		//当前登录人ID
		String faId = loginUser.getId();
		ResultMap result = new ResultMap();
		
		try {
			
			ProductSupplierDTO productSupplierDto=new ProductSupplierDTO();
			productSupplierDto.setFaId(faId==null?"":String.valueOf(faId));
			List<ProductSupplierDTO> queryProductSupplier = productSupplierService.queryProductSupplier(productSupplierDto);
		
			if(queryProductSupplier.size()>0){
				result.setAttribute("productSupplier",queryProductSupplier.get(0));
				result.setAttribute("flagProductSupplier","true");
			}else{
				result.setAttribute("flagProductSupplier","false");
			}
			
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			
		}catch(Exception e) {
			logger.warn("==>warn messang:"+e.getMessage());
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return result;
	} 
	
	/**
	 * 
	
	* @Title: editProductSupplierInfo
	
	* @Description:编辑产品供应方信息
	
	* @param @param request
	* @param @param response
	* @param @return    设定文件
	
	* @return ResultMap    返回类型
	
	* @throws
	 */
	@RequestMapping("editProductSupplierInfo")
	@ResponseBody
	public ResultMap editProductSupplierInfo(HttpServletRequest request, HttpServletResponse response,ProductSupplierDTO productSupplierDto){
		Session session = SessionUtil.getSession();
		UserDTO loginUser = session.getUser();
		//当前登录人ID
		String faId = loginUser.getId();
		ResultMap result = new ResultMap();
		try {
			
			String name = productSupplierDto.getName();
			String mobile = productSupplierDto.getMobile();
			String companyName = productSupplierDto.getCompanyName();
			String businessCardAddress = productSupplierDto.getBusinessCardAddress();
			boolean b = StringUtils.isBlank(name)||StringUtils.isBlank(mobile)
					||StringUtils.isBlank(companyName)||StringUtils.isBlank(businessCardAddress);
			if(b){
				logger.error("==>warn param is null ");
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_VALIDATE_ERROR);
				return result;
			}
			
			ProductSupplierDTO param=new ProductSupplierDTO();
			param.setFaId(faId==null?"":String.valueOf(faId));
			List<ProductSupplierDTO> queryProductSupplier = productSupplierService.queryProductSupplier(param);
			if(queryProductSupplier.size()==0){
				logger.info("==>info add productSupplierDto");
				productSupplierDto.setFaId(faId==null?"":String.valueOf(faId));
				DTOdateSetUtil.setUpdateInfo(productSupplierDto);
				DTOdateSetUtil.setCreateInfo(productSupplierDto);
				productSupplierService.addProductSupplier(productSupplierDto);
				result.setStatus(STATUS.SUCCESS);
				result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
				return result;
			}
			ProductSupplierDTO productSupplier = queryProductSupplier.get(0);
			//黑名单
			if("1".equals(productSupplier.getBlackList())){
				logger.info("==>warn getBlackList is 1 ");
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_ACCOUNT_BLACK);
				return result;
			}
			//审核通过
			if("1".equals(productSupplier.getCheckStatus())){
				logger.info("==>info getCheckStatus is 1 ");
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_PROHIBITION_MODIFY);
				return result;
			}
			DTOdateSetUtil.setUpdateInfo(productSupplierDto);
			productSupplierDto.setFaId(param.getFaId());
			productSupplierDto.setCheckStatus("0");
			productSupplierService.updateProductSupplier(productSupplierDto);
			
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		}catch(Exception e) {
			logger.warn("==>warn messang:"+e.getMessage());
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return result;
	} 
	
	/**
	 * 
	
	* @Title: editForegroundProduct
	
	* @Description:编辑产品信息    关联产品
	
	* @param @param request
	* @param @param response
	* @param @return    设定文件
	
	* @return ResultMap    返回类型
	
	* @throws
	 */
	@RequestMapping("editProductForForgroundPro")
	@ResponseBody
	public ResultMap editProductForForgroundPro(HttpServletRequest request, HttpServletResponse response,OutProductDTO outProductDto){
		Session session = SessionUtil.getSession();
		UserDTO loginUser = session.getUser();
		//当前登录人ID
		String faId = loginUser.getId();
		ResultMap result = new ResultMap();
		try {
			
			if(outProductDto==null||outProductDto.getId()==null){
				logger.error("==>warn outProductDto id is empty ");
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				return result;
			}
			BigDecimal getpAllSubscriptionAmount = outProductDto.getpAllSubscriptionAmount();
			String overTime = outProductDto.getOverTime();
			String getpPeriods = outProductDto.getpPeriods();
			
			BigDecimal getpIssuingScale = outProductDto.getpIssuingScale();
			boolean flag=StringUtils.isBlank(overTime)
			||getpAllSubscriptionAmount.doubleValue()<0||StringUtils.isBlank(getpPeriods)
			||getpIssuingScale.doubleValue()<0;
			
			if(flag){
				logger.error("==>warn outProductDto id is empty ");
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				return result;
			}
			
			ProductSupplierDTO queryDto=new ProductSupplierDTO();
			queryDto.setFaId(faId==null?"":String.valueOf(faId));
			List<ProductSupplierDTO> queryProductSupplier = productSupplierService.queryProductSupplier(queryDto);
			if(queryProductSupplier.size()==0){
				logger.error("==>warn queryProductSupplier is empty ");
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_OBJECT_NOFOUNT);
				return result;
			}
			ProductSupplierDTO productSupplier = queryProductSupplier.get(0);
			//黑名单
			if("1".equals(productSupplier.getBlackList())){
				logger.info("==>warn getBlackList is 1 ");
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_ACCOUNT_BLACK);
				return result;
			}
			
			ForegroundProductDTO param=new ForegroundProductDTO();
			param.setFaId(queryDto.getFaId());
			param.setId(outProductDto.getId());
			List<ForegroundProductDTO> queryForegroundProduct = foregroundProductService.queryForegroundProduct(param);
			if(queryForegroundProduct.size()==0){
				logger.info("==>info queryForegroundProduct.size() is 0");
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_OPERATE_ERROE);
				return result;
			}
			
			ForegroundProductDTO foregroundProductDTO = queryForegroundProduct.get(0);
			String checkStatus = foregroundProductDTO.getCheckStatus();
			if(!"4".equals(checkStatus)){
				logger.info("==>info getCheckStatus not is 5");
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_NOT_PUBLIC);
				return result;
			}else if(!"03".equals(foregroundProductDTO.getpSaleStatus())){
				logger.info("==>info 非募集中");
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_NOT_UPDATE);
				return result;
			}
			
			BigDecimal getpAllIssuingScale = foregroundProductDTO.getpAllIssuingScale();
			if(getpIssuingScale.compareTo(getpAllIssuingScale)==1){
				logger.info("单期规模不能大于发行规模");
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_PARAM_ERROR);
				return result;
			}
			if(getpAllSubscriptionAmount.compareTo(getpIssuingScale)==1){
				logger.info("已销售总额不能大于单期规模");
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_PARAM_ERROR);
				return result;
			}
			
			String sale=new StringBuffer("本期是第").append(getpPeriods).append("期，本期募集规模")
			.append(getpIssuingScale.divide(new BigDecimal(10000))).append("万，目前已进款").append(getpAllSubscriptionAmount.divide(new BigDecimal(10000)))
			.append("万，").append(overTime).append("封帐成立。").toString();
			outProductDto.setpRecruitmentSummary(sale);
			outProductDto.setpId(foregroundProductDTO.getpId());
			
			DTOdateSetUtil.setUpdateInfo(outProductDto);
			DTOdateSetUtil.setCreateInfo(outProductDto);
			DTOdateSetUtil.setUpdateInfo(foregroundProductDTO);
			outProductDto.setOldpIssuingScale(getpIssuingScale);
			outProductDto.setpExpectSubscriptionAmount(getpIssuingScale);
			foregroundProductService.updateOutProduct(outProductDto,foregroundProductDTO);
			
			try {
				//插入邮件待发送
				if(!StringUtils.isBlank(foregroundProductDTO.getAccessEmail())){
					Map<String,Object> params=new HashMap<String, Object>();
					params.put("publicDate",foregroundProductDTO.getPublicDate());
					params.put("productName",foregroundProductDTO.getProductName());
					params.put("pIssuingScale",sale);
					params.put("oldPIssuingScale",foregroundProductDTO.getpRecruitmentSummary());
					Map<String,Object> m  = MessageUtil.getMessage("outProductEmail","productEmail",params);
					String title = (String)m.get(MessageUtil.KEY_TITLE);
					String content = (String)m.get(MessageUtil.KEY_CONTENT);
					MessageMailDTO mail = new MessageMailDTO();
					mail.setFpMailContent(content);
					mail.setFpMailTitle(title);
					mail.setFpMailTo(foregroundProductDTO.getAccessEmail());
					mail.setFpMailType("outProductEmail");
					mail.setFpMailSendStatus(Constants.message_status_not_send);
					mail.setStatus(Constants.status_normal);
					mail.setFpMailSendType(Constants.message_type_now);
					mail.setCreateBy(outProductDto.getCreateBy());
					mail.setUpdateBy(outProductDto.getCreateBy());
					mail.setFpMailSendTimes(0);
					messageService.insertMailInfo(mail);
				}
			} catch (Exception e) {
				logger.error("send email error",e);
			}
			
			logger.info("updateOutProduct.info:"+outProductDto.getpRecruitmentSummary());
			logger.info("updateOutProduct.info sale :"+sale);
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		}catch(Exception e) {
			logger.error("error",e);
			logger.warn("==>warn messang:"+e.getMessage());
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return result;
	} 
	/**
	 * 
	
	* @Title: queryForegroundProductList
	
	* @Description:查询用户产品供应列表
	
	* @param @param request
	* @param @param response
	* @param @return    设定文件
	
	* @return ResultMap    返回类型
	
	* @throws
	 */
	@RequestMapping("queryForegroundProductList")
	@ResponseBody
	public ResultMap queryForegroundProductList(HttpServletRequest request, HttpServletResponse response, Pager pager){
		Session session = SessionUtil.getSession();
		UserDTO loginUser = session.getUser();
		//当前登录人ID
		String faId = loginUser.getId();
		ResultMap result = new ResultMap();
		try {
			ForegroundProductDTO foregroundProductDto=new ForegroundProductDTO();
			foregroundProductDto.setFaId(faId==null?"":String.valueOf(faId));
			int countForegroundProduct = foregroundProductService.countForegroundProduct(foregroundProductDto);
			pager.setTotalCount(countForegroundProduct);
			foregroundProductDto.setPager(pager);
			List<ForegroundProductDTO> foregroundProductList = foregroundProductService.queryForegroundProduct(foregroundProductDto);
			result.setAttribute("foregroundProductList", foregroundProductList);
			result.setAttribute("pager", pager);
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		}catch(Exception e) {
			logger.warn("==>warn messang:"+e.getMessage());
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return result;
	} 
	
	@RequestMapping("queryForegroundProductListById")
	@ResponseBody
	public ResultMap queryForegroundProductList(HttpServletRequest request, HttpServletResponse response, int id){
		Session session = SessionUtil.getSession();
		UserDTO loginUser = session.getUser();
		//当前登录人ID
		String faId = loginUser.getId();
		ResultMap result = new ResultMap();
		
		if(id <= 0){
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_VALIDATE_ERROR);
			return result;
		}		
		
		try {
			ForegroundProductDTO foregroundProductDto=new ForegroundProductDTO();
			foregroundProductDto.setFaId(faId==null?"":String.valueOf(faId));
			foregroundProductDto.setId(id);
			List<ForegroundProductDTO> foregroundProductList = foregroundProductService.queryForegroundProduct(foregroundProductDto);
			result.setAttribute("foregroundProductList", foregroundProductList);
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		}catch(Exception e) {
			logger.warn("==>warn messang:"+e.getMessage());
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return result;
	} 
	
}
